package com.test.prolenders.analytics_prolenders.data_sdk_prolenders;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.logger_prolenders.ASLger_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcAppsTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcBtyTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcClogTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcCntaTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcHadwTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcSsTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcStorTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASDevcWfiTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.ASFilterOdsTsk_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.OnDevcTskCmple_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.task_prolenders.OnFilterOdsTskCmpt_prolenders;
import com.test.prolenders.analytics_prolenders.data_sdk_prolenders.util_prolenders.ASUtil_prolenders;

import java.util.concurrent.CompletableFuture;

public class ASSycManager_prolenders {
    private final static String TAG = "ASSycManager";

    private ASSycManager_prolenders() {
    }

    @SuppressLint("StaticFieldLeak")
    private static ASSycManager_prolenders instance;

    public static ASSycManager_prolenders getInstance() {
        if (instance == null) {
            synchronized (ASSycManager_prolenders.class) {
                instance = new ASSycManager_prolenders();
            }
        }
        return instance;
    }

    public Context mContext;

    private OnDevcTskCmple_prolenders mOnDevcTskCmpleProlenders;

    public void init(Context applicationContext) {
        mContext = applicationContext;
    }

    public void sycData(OnDevcTskCmple_prolenders completeListener) {
        try {
            this.mOnDevcTskCmpleProlenders = completeListener;

            // 创建任务列表
            CompletableFuture<String> device1Task = CompletableFuture.supplyAsync(new ASDevcBtyTsk_prolenders(mContext, ASBuilder_prolenders.T_BTY));
            CompletableFuture<String> device2Task = CompletableFuture.supplyAsync(new ASDevcStorTsk_prolenders(mContext, ASBuilder_prolenders.T_STO));
            CompletableFuture<String> device3Task = CompletableFuture.supplyAsync(new ASDevcAppsTsk_prolenders(mContext, ASBuilder_prolenders.T_APL));
            CompletableFuture<String> device4Task = CompletableFuture.supplyAsync(new ASDevcWfiTsk_prolenders(mContext, ASBuilder_prolenders.T_NTW));
            CompletableFuture<String> device5Task = CompletableFuture.supplyAsync(new ASDevcHadwTsk_prolenders(mContext, ASBuilder_prolenders.T_HDW));
            CompletableFuture<String> device6Task = CompletableFuture.supplyAsync(new ASDevcTsk_prolenders(mContext, ASBuilder_prolenders.T_DIE));
            CompletableFuture<String> device7Task = CompletableFuture.supplyAsync(new ASDevcSsTsk_prolenders(mContext, ASBuilder_prolenders.T_MAG));
            CompletableFuture<String> device8Task = CompletableFuture.supplyAsync(new ASDevcCntaTsk_prolenders(mContext, ASBuilder_prolenders.T_COTACT));
            CompletableFuture<String> device9Task = CompletableFuture.supplyAsync(new ASDevcClogTsk_prolenders(mContext, ASBuilder_prolenders.T_CAL));

            // 提交任务
            CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(
                    device1Task,
                    device2Task,
                    device3Task,
                    device4Task,
                    device5Task,
                    device6Task,
                    device7Task,
                    device8Task,
                    device9Task);

            // 在所有任务触发后回调，不一定要等结果完成！！！
            allOfFuture.thenRun(() -> {
                // 日记标识
                ASLger_prolenders.d(TAG, "sycData: 在所有任务触发后回调");
                new Handler(Looper.getMainLooper()).post(() -> {
                    if (mOnDevcTskCmpleProlenders != null) {
                        mOnDevcTskCmpleProlenders.onAllCompleted();
                    }
                });

            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void filterOrders(OnFilterOdsTskCmpt_prolenders completeListener) {
        CompletableFuture<String> filterOrdersTask = CompletableFuture.supplyAsync(new ASFilterOdsTsk_prolenders(mContext));
        CompletableFuture<Void> allOfFuture = CompletableFuture.allOf(filterOrdersTask);
        allOfFuture.thenRunAsync(() -> {
            String result = ASUtil_prolenders.safeString(filterOrdersTask.join()).trim();

            // 日记标识
            ASLger_prolenders.d(TAG, "filterOrders: 在所有任务完成后回调");
            ASLger_prolenders.d(TAG, "filterOrders result: " + result);

            // 主线程回调
            new Handler(Looper.getMainLooper()).post(() -> {
                if (completeListener != null) {
                    completeListener.onAllCompleted(result);
                }
            });
        });
    }
}
